#include "DemoSceneScreen.hpp"
#include "../SceneManager.hpp"
#include "../Entities/SphereEntity.hpp"
#include "../Entities/TeapotEntity.hpp"
#include "../Camera/CameraObject.hpp"
#include "../Camera/CameraManager.hpp"

#include "../ResourceManagers/textfile.h"
#include "../ResourceManagers/TextureManager.hpp"
#include "../ResourceManagers/TextureObject.hpp"
#include "../ResourceManagers/HDRManager.hpp"
#include "../ResourceManagers/HDRImageObject.hpp"

#include <assert.h>

#include "../HDRDemoConstants.hpp"



DemoSceneScreen::DemoSceneScreen() :
m_camera1( NULL ),
m_camera2( NULL )
{
	//! Set type of screen.
	m_type = SCREEN_SCENE;
		
	//! Create and add two new cameras.
	//float fov = 45.0f; float near = 1.0f; float far = 1000.0f;
 
	m_camera1 = new CameraObject( MVector3D( 0.0f, 0.0f, 0.0f ), MVector3D( 0.0f, 1.0f, 0.0f ),
							MVector3D( 1.0f, 0.0f, 0.0f ), MVector3D( 0.0f, 10.0f, 15.0f ), 
							45.0, 1.0, 50.0 );
							//fov, near, far );

	m_camera2 = new CameraObject( MVector3D( 0.0f, 0.0f, 0.0f ), MVector3D( 0.0f, 1.0f, 0.0f ),
							MVector3D( 1.0f, 0.0f, 0.0f ), MVector3D( 0.0f, 11.0f, 9.0f ), 
							45.0, 1.0, 50 );
							//fov, near, far );
	//CameraObject(

	CameraManager::GetInstance()->RegisterCamera( m_camera1 );
	CameraManager::GetInstance()->RegisterCamera( m_camera2 );

	//! Set m_camera2 as active camera (scene will be rendered from its point of view).
	m_camera2->ActivateCamera();

	//! Initialize entities for this scene.
	InitializeEntities();

	glClearColor(0.4f, 0.4f, 0.4f, 0.0f);
}

DemoSceneScreen::~DemoSceneScreen()
{
	//! Unregister cameras so they can be destroyed.
	CameraManager::GetInstance()->UnregisterAllCameras();


	//! temporary stuff
	HDRManager::getInstance()->unloadHDR("grace_probe.hdr");
	TextureManager::getInstance()->unloadTexture("grace_probe.hdr");
}

bool DemoSceneScreen::InitializeEntities()
{
	/*SphereEntity* sphere1 = new SphereEntity;
	sphere1->SetPosition( MVector3D( -1.5f, 2.5f, -3.0f ) );
	sphere1->SetDiffuseMaterial( MVector3D( 0.5f, 0.5f, 0.5f ) );
	sphere1->SetSpecularMaterial( MVector3D( 1.0f, 1.0f, 1.0f ) );
	sphere1->SetShininess( 20.0f );
	SceneManager::GetInstance()->RegisterEntity( sphere1 );*/

	SphereEntity* sphere2 = new SphereEntity;
	sphere2->SetPosition( MVector3D( 0.0f, 2.0f, 0.0f ) );
	sphere2->SetDiffuseMaterial( MVector3D( 0.9f, 0.1f, 0.1f ) );
	sphere2->SetSpecularMaterial( MVector3D( 1.0f, 1.0f, 1.0f ) );
	sphere2->SetShininess( 5.0f );
	SceneManager::GetInstance()->RegisterEntity( sphere2 );

	HDRManager::getInstance()->loadHDR("grace_probe.hdr");
	const HDRImageObject* pHDR = HDRManager::getInstance()->getHDR("grace_probe.hdr");
	TextureManager::getInstance()->loadTexture(pHDR);


	/*SphereEntity* sphere3 = new SphereEntity;
	//TeapotEntity* teapot1 = new Teapot;
	sphere3->SetPosition( MVector3D( 0.5f, 6.0f, 2.5f ) );
	sphere3->SetScale( 1.0f );
	sphere3->SetDiffuseMaterial( MVector3D( 0.0f, 0.3f, 0.7f ) );
	sphere3->SetSpecularMaterial( MVector3D( 1.0f, 1.0f, 1.0f ) );
	sphere3->SetShininess( 5.0f );
	SceneManager::GetInstance()->RegisterEntity( sphere3 );


	SphereEntity* sphere4 = new SphereEntity;
	sphere4->SetPosition( MVector3D( -2.0f, 2.0f, -1.0f ) );
	sphere4->SetDiffuseMaterial( MVector3D( 0.6f, 0.6f, 0.1f ) );
	sphere4->SetSpecularMaterial( MVector3D( 1.0f, 1.0f, 1.0f ) );
	sphere4->SetShininess( 10.0f );
	SceneManager::GetInstance()->RegisterEntity( sphere4 );*/

	return true;
}

bool DemoSceneScreen::CleanUpEntities()
{
	SceneManager::GetInstance()->UnregisterAllEntities();

	return true;
}